package mainpackage;

/*
 * Class Name: Pern
 */
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Pern {
	private Scanner kb = new Scanner(System.in);
	private Weyr benden;

	public Pern() {
		benden = new Weyr();
	}

	private void run() throws IOException {
		char choice;
		do {
			menu();
			updateHours();
			choice = kb.next().charAt(0);
			switch (choice) {
			case 'a':
				addRider();
				break;
			case 's':
				displayRiderDetail();
				break;
			case 'm':
				setMission();
				break;
			case 'u':
				updateStatus();
				break;
			case 'e':
				endMission();
				break;
			case 'r':
				readFromFile();
				break;
			case 'q':
				System.out.println("quit the system");
				break;
			default:
				System.out.println("Invalid choice");
				break;
			}
		} while (choice != 'q' && choice!='Q');
	}

	private void menu() {
		System.out.println("\n\tMain Menu\n");
		System.out.println("\t a - Add Rider");
		System.out.println("\t s - Show All Riders ");
		System.out.println("\t r - Read from file");
		System.out.println("\t m - Set mission");
		System.out.println("\t u - Update status");
		System.out.println("\t e - End Mission");
		System.out.println("\t q - Quit");
		System.out.print("\t\tEnter choice >> ");
	}

	private void addRider() {
		/*
		 * Ask the user for the name and code of a Rider Create the Rider object
		 * call benden.addRider( ... );
		 */
		String name,code;
		System.out.println("please input the rider's name = ");
		name = kb.next();
		System.out.println("please input the rider's code = ");
		code = kb.next();
		Rider newrider = new Rider(name,code);
		benden.addRider(newrider);
	}

	private void displayRiderDetail(){
		benden.displayRiderDetail();
	}
	
	private void readFromFile() throws FileNotFoundException{
		String filename;
		System.out.println("please input the file name which contains riders infomation:");
		filename = kb.next();//user should input ridersinfo.txt
		benden.loadFromFile(filename);
	}
	
	private void setMission(){
		String name;
		int type;
		System.out.println("please input the rider's name = ");
		name = kb.next();
		System.out.println("please input the mission type = ");
		type = kb.nextInt();
		benden.mission(name, type);
		
	}
	
	private void endMission(){
		String name;
		System.out.println("please input the rider's name = ");
		name = kb.next();
		benden.endMission(name);
	}
	
	private void updateStatus(){
		String name;
		int newStatus;
		System.out.println("please input the rider's name = ");
		name = kb.next();
		System.out.println("please input the new status = ");
		newStatus = kb.nextInt();
		System.out.println( benden.setStatus(name,newStatus) );
	}
	
	public void updateHours(){
		benden.updateHours();
	}
	
	public static void main(String[] args) throws IOException {
		Pern p = new Pern();
		p.run();
	}
}